#delimit ;

**********************************;
*** x_pay_i: Caluculated amount of child benefit for i-th child;
**********************************;
gen x_pay_1=0;
gen x_pay_2=0;
gen x_pay_3=0;
gen x_pay_4=0;
gen x_pay_5=0;
gen x_pay_6=0;
gen x_pay_7=0;
gen x_pay_8=0;
gen x_pay_9=0;
gen x_pay_10=0;


replace x_pay_1=1*5*(child_1<1&child_1>=0) if period==m(1992m2);
replace x_pay_1=4*5*(child_1<1&child_1>=0) if period==m(1992m6)|period==m(1992m10);
replace x_pay_1=3*5*(child_1<1&child_1>=0)+5*(child_1==1) if period==m(1993m2);
replace x_pay_1=4*5*(child_1<2&child_1>=0) if period==m(1993m6)|period==m(1993m10);
replace x_pay_1=3*5*(child_1<2&child_1>=0)+5*(child_1==2) if period==m(1994m2);

replace x_pay_2=4*2.5*(child_2<3&child_2>=0) if period==m(1986m10)|period==m(1987m2);

replace x_pay_2=4*2.5*(child_2<4&child_2>=0)+2*2.5*(child_2==4) if period==m(1987m6);
replace x_pay_2=4*2.5*(child_2<5&child_2>=0) if period==m(1987m10)|period==m(1988m2);
replace x_pay_2=4*2.5*(child_2<5&child_2>=0)+2*2.5*((child_2==6|child_2==5)&(child_s_2==1|child_s_2==0)) if period==m(1988m6);
replace x_pay_2=4*2.5*((child_2<6&child_2>=0)|(child_2==6&(child_s_2==1|child_s_2==0))) if period>m(1988m6)&period<m(1992m2)&(month==2|month==6|month==10);

replace x_pay_2=3*2.5*((child_2<6&child_2>=0)|(child_2==6&(child_s_2==1|child_s_2==0))) if period==m(1992m2);
replace x_pay_2=4*5*(child_2<5&child_2>=0) if period==m(1992m6)|period==m(1992m10);
replace x_pay_2=3*5*(child_2<5&child_2>=0)+5*(child_2<4&child_2>=0) if period==m(1993m2);
replace x_pay_2=4*5*(child_2<4&child_2>=0) if period==m(1993m6)|period==m(1993m10);
replace x_pay_2=3*5*(child_2<4&child_2>=0)+5*(child_2<3&child_2>=0) if period==m(1994m2);
	

forvalues k=1/2{;
	replace x_pay_`k'=4*5*(child_`k'<3&child_`k'>=0) if period>=m(1994m6)&period<m(2000m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*5*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=3))+4*5*(child_`k'<3&child_`k'>=0) if period==m(2000m6);
	replace x_pay_`k'=4*5*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=0)) if period>m(2000m6)&period<m(2004m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*5*((child_`k'<10&child_`k'>6)|(child_`k'==6&child_s_`k'==2))+4*5*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=0)) if period==m(2004m6);
	replace x_pay_`k'=4*5*(child_`k'<10&child_`k'>=0) if period>=m(2004m10)&period<m(2007m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*5*((child_`k'<12&child_`k'>=9)|(child_`k'==12&child_s_`k'==2))+4*5*(child_`k'<10&child_`k'>=0)+2*5*(child_`k'<3&child_`k'>=0) if period==m(2007m6);
	replace x_pay_`k'=4*5*((child_`k'==12&child_s_`k'==2)|(child_`k'<12&child_`k'>=0))+4*5*(child_`k'<3&child_`k'>=0) if period>=m(2007m10)&period<m(2010m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*13*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>12)|(child_`k'==12&child_s_`k'==3))+(2*5+13*2)*((child_`k'==12&child_s_`k'==2)|(child_`k'<12&child_`k'>=0))+2*5*(child_`k'<3&child_`k'>=0) if year==2010&month==6;
	replace x_pay_`k'=4*13*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0)) if (year==2010&month==10)|(year==2011&month==2)|(year==2011&month==6)|(year==2011&month==10);
	replace x_pay_`k'=4*10*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0))+4*5*(child_`k'<3&child_`k'>=0) if year>=2012&(month==2|month==6|month==10);
};
forvalues k=3/10{;
	replace x_pay_`k'=4*5*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0)) if period<=m(1987m2)&(month==2|month==6|month==10);

	replace x_pay_`k'=2*5*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0))+2*5*(child_`k'<9&child_`k'>=0) if period==m(1987m6);
	replace x_pay_`k'=4*5*(child_`k'<9&child_`k'>=0) if period==m(1987m10)|period==m(1988m2);
	replace x_pay_`k'=2*5*(child_`k'<9&child_`k'>=0)+2*5*((child_`k'==6&child_s_`k'<2)|(child_`k'<=5&child_`k'>=0)) if period==m(1988m6);
	replace x_pay_`k'=4*5*((child_`k'==6&child_s_`k'<2)|(child_`k'<=5&child_`k'>=0)) if period>=m(1988m6)&period<m(1992m2)&(month==2|month==6|month==10);
	replace x_pay_`k'=3*5*((child_`k'==6&child_s_`k'<2)|(child_`k'<5&child_`k'>=0))+10*(child_`k'<5&child_`k'>=0) if period==m(1992m2);
	
	replace x_pay_`k'=4*10*(child_`k'<5&child_`k'>=0) if period==m(1992m6)|period==m(1992m10);
	replace x_pay_`k'=4*10*(child_`k'<4&child_`k'>=0)+2*10*(child_`k'==5) if period==m(1993m2);
	replace x_pay_`k'=4*10*(child_`k'<4&child_`k'>=0) if period==m(1993m6)|period==m(1993m10);
	replace x_pay_`k'=4*10*(child_`k'<3&child_`k'>=0)+2*10*(child_`k'==4) if period==m(1994m2);		

	replace x_pay_`k'=4*10*(child_`k'<3&child_`k'>=0) if period>=m(1994m6)&period<m(2000m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*10*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=3))+4*10*(child_`k'<3&child_`k'>=0) if period==m(2000m6);	
	replace x_pay_`k'=4*10*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=0)) if period>m(2000m6)&period<m(2004m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*10*((child_`k'<10&child_`k'>6)|(child_`k'==6&child_s_`k'==2))+4*10*((child_`k'==6&(child_s_`k'==1|child_s_`k'==0))|(child_`k'<6&child_`k'>=0)) if period==m(2004m6);
	replace x_pay_`k'=4*10*(child_`k'<10&child_`k'>=0) if period>=m(2004m10)&period<m(2007m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*5*((child_`k'<12&child_`k'>=9)|(child_`k'==12&child_s_`k'==2))+4*10*(child_`k'<10&child_`k'>=0) if period==m(2007m6);
	
	replace x_pay_`k'=4*10*((child_`k'==12&child_s_`k'==2)|(child_`k'<12&child_`k'>=0)) if period>=m(2007m10)&period<m(2010m6)&(month==2|month==6|month==10);
	replace x_pay_`k'=2*13*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>12)|(child_`k'==12&child_s_`k'==3))+(2*10+2*13)*((child_`k'==12&child_s_`k'==2)|(child_`k'<12&child_`k'>=0)) if year==2010&month==6;
	replace x_pay_`k'=4*13*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0)) if (year==2010&month==10)|(year==2011&month==2)|(year==2011&month==6)|(year==2011&month==10);
	replace x_pay_`k'=4*15*((child_`k'==15&child_s_`k'==3)|(child_`k'<15&child_`k'>=0)) if year>=2012&(month==2|month==6|month==10);
};

**********************************;
*** x_pay: Total amount of child benefit;
**********************************;
gen x_pay=0;
forvalues k=1/10{;
	replace x_pay=x_pay+x_pay_`k';
};
drop x_pay_*;

#delimit cr

***** Earning Test *********

*** Estimating the annual income of household head
******* Earning test is applicable to the income of head
******* Only household total income is available in the Annual Income Questionnaire
******* In another questionnaire, monthly income by earner is available
******* Using the information, we calculate the annual income earned by head
******* Ratio of Head Income to Household Total Income are as below
gen head_y=yearly_income
replace head_y=head_y*462/564 if year==1992
replace head_y=head_y*468/571 if year==1993
replace head_y=head_y*468/567 if year==1994
replace head_y=head_y*468/571 if year==1995
replace head_y=head_y*475/579 if year==1996
replace head_y=head_y*487/595 if year==1997
replace head_y=head_y*480/589 if year==1998
replace head_y=head_y*468/575 if year==1999
replace head_y=head_y*460/563 if year==2000
replace head_y=head_y*448/553 if year==2001
replace head_y=head_y*439/540 if year==2002
replace head_y=head_y*430/525 if year==2003
replace head_y=head_y*436/532 if year==2004
replace head_y=head_y*426/525 if year==2005
replace head_y=head_y*431/526 if year==2006
replace head_y=head_y*433/529 if year==2007
replace head_y=head_y*434/534 if year==2008
replace head_y=head_y*419/518 if year==2009


******* Tested Income should be calculated as follows
gen test_y=max((job<5)*max(head_y*0.95-(head_y<10000)*head_y*0.05-(head_y<6600)*head_y*0.1-(head_y<3600)*head_y*0.1-(head_y<1800)*head_y*0.1,max(head_y-650,0))+(job>=5)*head_y-80,0)
drop head_y

******* Threthold depends on number of dependents and caluculated as follows

#delimit ;

gen line= (period>=m(1981m1)&period<m(1982m1)      )*(1700+(1-(job_spouse!=10)+eligible_18)*290+(number_member_65)*350)
         +(period>=m(1982m1)&period<m(1983m1)      )*(1228+(1-(job_spouse!=10)+eligible_18)*290+(number_member_65)*350)
         +(period>=m(1983m1)&period<m(1984m1)      )*(1235+(1-(job_spouse!=10)+eligible_18)*290+(number_member_65)*350)
         +(period>=m(1984m1)&period<m(1985m1)      )*(1258+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1985m1)&period<m(1986m1)      )*(1280+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1986m1)&period<m(1987m1)&job> 4)*(1330+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1986m1)&period<m(1987m1)&job<=4)*(3075+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
        
         +(period>=m(1987m1)&period<m(1988m1)&job> 4)*(1336+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1987m1)&period<m(1988m1)&job<=4)*(3255+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1988m1)&period<m(1989m1)&job> 4)*(1344+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1988m1)&period<m(1989m1)&job<=4)*(3345+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1989m1)&period<m(1990m1)&job> 4)*(1387+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1989m1)&period<m(1990m1)&job<=4)*(3435+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1990m1)&period<m(1995m6)&job> 4)*(1476+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1990m1)&period<m(1995m6)&job<=4)*(3630+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1995m6)&period<m(1996m6)&job> 4)*(1486+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1995m6)&period<m(1996m6)&job<=4)*(3278+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(1996m6)&period<m(2001m6)&job> 4)*(1496+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)
         +(period>=m(1996m6)&period<m(2001m6)&job<=4)*(3278+(1-(job_spouse!=10)+eligible_18)*300+(number_member_65)*360)

         +(period>=m(2001m6)&period<m(2002m6)&job> 4)*(1700+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)
         +(period>=m(2001m6)&period<m(2002m6)&job<=4)*(3610+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)

         +(period>=m(2002m6)&period<m(2006m6)&job> 4)*(3010+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)
         +(period>=m(2002m6)&period<m(2006m6)&job<=4)*(4600+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)

         +(period>=m(2006m6)&period<m(2010m6)&job> 4)*(4600+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)
         +(period>=m(2006m6)&period<m(2010m6)&job<=4)*(5320+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440)

         +(period>=m(2010m6)&period<m(2012m6)       )*(99999)

         +(period>=m(2012m6)                        )*(6220+(1-(job_spouse!=10)+eligible_18)*380+(number_member_65)*440);


#delimit cr


